﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Clipping.Infrastructure.Models;
using System.IO;
using Clipping.Utils;
using BLToolkit.Data.Linq;
namespace ClippingMaster
{
    class KeywordsRepository
    {
        static DBConnectionPool keywordDBConnectionPool;
        static KeywordsRepository()
        {
            string keywordDBPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DB", "keywords.db");
            keywordDBConnectionPool = DBHelper.CreateDBConnectionPool<KeywordDBUpdater>(keywordDBPath, 10);
        }
        public bool AddCategory(KeywordCategory category)
        {
            using (DBConnection conn = keywordDBConnectionPool.GetConnection())
            {
                var rtn= KEYWORDSCATEGORY.Records.Insert(() => new KEYWORDSCATEGORY
                {
                    ID = category.Id.ToString(),
                    Name = category.Name,
                    ParentID = category.ParentId.ToString()
                }) != 0;
                conn.Free();
                return rtn;
            }

        }
        public bool UpdateCategory(KeywordCategory category)
        {
            using (DBConnection conn =keywordDBConnectionPool.GetConnection())
            {
                var rtn= KEYWORDSCATEGORY.Records.Where(x => x.ID == category.Id.ToString()).Update(x => new KEYWORDSCATEGORY
                {
                    ID = category.Id.ToString(),
                    Name = category.Name,
                    ParentID = category.ParentId.ToString()
                }) != 0;
                conn.Free();
                return rtn;
            }
        }
        public bool DelCategory(Guid categoryId)
        {
            using (DBConnection conn = keywordDBConnectionPool.GetConnection())
            {
                var result = KEYWORDSCATEGORY.Records.Delete(x => x.ID == categoryId.ToString());
                conn.Free();
                return result == 1;
            }
        }
        public List<KeywordCategory> GetAllCategories()
        {
            using (DBConnection conn = keywordDBConnectionPool.GetConnection())
            {
                var items = KEYWORDSCATEGORY.Records;
                List<KeywordCategory> rtn = new List<KeywordCategory>();
                if (items != null)
                {
                    foreach (var i in items)
                    {
                        KeywordCategory kc = new KeywordCategory() { Id = Guid.Parse(i.ID), ParentId = Guid.Parse(i.ParentID), Name = i.Name };
                        rtn.Add(kc);
                    }
                }
                conn.Free();
                return rtn;
            }
        }

        public bool AddKeyword(KeywordInfo keyword)
        {
            using (DBConnection conn = keywordDBConnectionPool.GetConnection())
            {
                var rtn= KEYWORDS.Records.Insert(() => new KEYWORDS
                {
                    ID = keyword.Id.ToString(),
                    CategoryId = keyword.CategoryId.ToString(),
                    Keyword = keyword.Keyword,
                    SearchPeriod = (int)keyword.SearchPeriod,
                    SearchType = (int)keyword.SearchType
                }) != 0;
                conn.Free();
                return rtn;
            }
        }
        public bool UpdateKeyword(KeywordInfo keyword)
        {
            using (DBConnection conn = keywordDBConnectionPool.GetConnection())
            {
                var rtn= KEYWORDS.Records.Where(x => x.ID == keyword.Id.ToString()).Update(x => new KEYWORDS
                {
                    ID = keyword.Id.ToString(),
                    CategoryId = keyword.CategoryId.ToString(),
                    Keyword = keyword.Keyword,
                    SearchPeriod = (int)keyword.SearchPeriod,
                    SearchType = (int)keyword.SearchType
                }) != 0;
                conn.Free();
                return rtn;
            }
        }
        public bool DeleteKeyword(KeywordInfo keyword)
        {
            using (DBConnection conn = keywordDBConnectionPool.GetConnection())
            {
                var result = KEYWORDS.Records.Delete(x => x.ID == keyword.Id.ToString());
                conn.Free();
                return result == 1;
            }
        }
        public List<KeywordInfo> GetAllKeywords()
        {
            using (DBConnection conn = keywordDBConnectionPool.GetConnection())
            {
                var items = KEYWORDS.Records;
                List<KeywordInfo> rtn = new List<KeywordInfo>();
                if (items != null)
                {
                    foreach (var i in items)
                    {
                        KeywordInfo kc = new KeywordInfo() { Id = Guid.Parse(i.ID), CategoryId = Guid.Parse(i.CategoryId), Keyword = i.Keyword, SearchType = (SearchType)i.SearchType, SearchPeriod = (SearchPeriod)i.SearchPeriod };
                        rtn.Add(kc);
                    }
                }
                conn.Free();
                return rtn;
            }
        }
    }
}
